package com.magenta.maxoptra.gis;

import com.magenta.maxoptra.gis.utils.DateTimeHelper;

import java.io.Serializable;

/*
* Project: MaxOptRA v1.1
* User: Ivan Usalko
* Date: 19.01.2009 12:49:15
*
* Copyright (c) 1999-2009 Magenta Corporation Ltd. All Rights Reserved.
* Magenta Technology proprietary and confidential.
* Use is subject to license terms.
*/
/**
 * Leg by map.
 */
public class GeoRoute implements Serializable {

    double distanceInKM;
    long durationInMillis;
    GeographicCoordinates from;
    GeographicCoordinates to;
    Source source = null;
    VehicleType vehicleType = VehicleType.Default;
    public static final Long MAX_DURATION = DateTimeHelper.MSECS_IN_DAY * 7;
    long requestDuration;

    public GeoRoute(double distanceInKM, long durationInMillis, GeographicCoordinates from, GeographicCoordinates to) {
        this.distanceInKM = distanceInKM;
        if (durationInMillis > MAX_DURATION) {
            this.durationInMillis = MAX_DURATION;
        } else {
            this.durationInMillis = durationInMillis;
        }
        this.from = from;
        this.to = to;
    }

    public GeoRoute(double distanceInKM, long durationInMillis, GeographicCoordinates from, GeographicCoordinates to, long requestDuration) {
        this(distanceInKM, durationInMillis, from, to);
        this.requestDuration = requestDuration;
    }

    public GeoRoute(double distanceInKM, long durationInMillis, GeographicCoordinates from, GeographicCoordinates to, VehicleType vehicleType) {
        this(distanceInKM, durationInMillis, from, to);
        this.vehicleType = vehicleType;
    }

    public double getDistanceInKM() {
        return distanceInKM;
    }

    public long getDurationInMillis() {
        return durationInMillis;
    }

    public long getDurationInSecs() {
        return durationInMillis / (DateTimeHelper.MSECS_IN_SECOND);
    }

    public double getDurationInMinutes() {
        return durationInMillis / (DateTimeHelper.MSECS_IN_MINUTE);
    }

    public GeographicCoordinates getFrom() {
        return from;
    }

    public GeographicCoordinates getTo() {
        return to;
    }

    private String geoAddressToString(GeographicCoordinates coords) {
        if (coords != null) {
            return String.format("{%f:%f}", coords.getLongitude(), coords.getLatitude());
        }
        return null;
    }

    @Override
    public String toString() {
        StringBuffer out = new StringBuffer();
        out.append(GeoRoute.class.getSimpleName()).append(":");
        out.append(" ").append(geoAddressToString(from)).append("->").append(geoAddressToString(to));
        out.append("[");
        out.append(String.format("%f km", distanceInKM)).append(", ");
        out.append(parceDuration());
        out.append("]");
        out.append(" // {longitude:latitude}");
        return out.toString();
    }

    private String parceDuration() {
        long sec = durationInMillis / 1000;
        long ms = durationInMillis - sec * 1000;
        long mins = sec / 60;
        sec -= mins * 60;
        long hours = mins / 60;
        mins -= hours * 60;
        String s = "";
        if(hours > 0) s+=hours + "h: ";
        if(mins > 0) s+=mins + "m: ";
        if(sec > 0) s+=sec + "s: ";
        s+= ms + "ms";
        return s;
    }

    public static GeoRoute zeroRoute(GeographicCoordinates coords) {
        return new GeoRoute(0d, 0l, coords, coords);
    }

    public Source getSource() {
        return source;
    }

    public void setSource(Source source) {
        this.source = source;
    }

    public VehicleType getVehicleType() {
        return vehicleType;
    }

    public void setVehicleType(VehicleType vehicleType) {
        this.vehicleType = vehicleType;
    }

    public long getRequestDuration() {
        return requestDuration;
    }

    public void setRequestDuration(long requestDuration) {
        this.requestDuration = requestDuration;
    }

    public enum Source {
        EXTERNAL_PROVIDER,
        CACHE,
        APPROXIMATION
    }

}
